package com.li.onjava8.concurrent.pl;

import java.util.Arrays;

/**
 * @author li
 * @date 2023/10/30 10:20
 */
public class Summing3 {
    static long basicSum(Long[] ia) {
        long sum = 0;
        int size = ia.length;
        for(int i = 0; i < size; i++)
            sum += ia[i];
        return sum;
    }

    public static final int SZ = 10_000_000;
    public static final long CHECK = (long)SZ * ((long)SZ + 1)/2;

    public static void main(String[] args) {
        Long[] arr = new Long[SZ + 1];
        Arrays.parallelSetAll(arr, i -> (long)i);
        Summing.timeTest("Long Array Stream Reduce", CHECK, () -> Arrays.stream(arr).reduce(0L, Long::sum));
        Summing.timeTest("Long Array Basic Sum", CHECK, () -> basicSum(arr));
        Summing.timeTest("Long Array ParallelPrefix", CHECK, () -> {
            Arrays.parallelPrefix(arr, Long::sum);
            return arr[arr.length - 1];
        });
    }


}
